package com.nit.lab.controller;


import com.nit.lab.dto.DtoOrderAllInfo;
import com.nit.lab.dto.DtoStore;
import com.nit.lab.dto.DtohisOrder;
import com.nit.lab.dto.utils.DtoAjaxMsg;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.dto.utils.DtoDataTableReturn;
import com.nit.lab.service.OrderService;
import com.nit.lab.utils.SystemConstants;
import org.jboss.logging.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Scope("prototype")
@RequestMapping("/orderController")
public class OrderController {

    private static Logger log = Logger.getLogger(UserController.class.getClass());

    @Resource
    private OrderService orderService;

    @ResponseBody
    @RequestMapping("/creatOrder")
    public DtoAjaxMsg creatOrder(DtoOrderAllInfo dtoOrderAllInfo, Integer creat) {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result = orderService.creatOrder(dtoOrderAllInfo, creat);
            if (result > SystemConstants.Insert_Success) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_SUCCESS);
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
        }
        return dtoAjaxMsg;
    }

    @ResponseBody
    @RequestMapping("/getUnOrder")
    public DtoDataTableReturn getUnOrder(DtoDataTable dtoDataTable, Integer sessionId, Integer actorId) {
        DtoDataTableReturn jsonList = null;
        Integer pre;
        try {
            if (sessionId == null) {
                pre = -1;
            } else {
                pre = sessionId;/*帐号*/
            }
            Integer count = orderService.getHisOrderDataTableCount(dtoDataTable.getSearch(), pre, actorId);
            List<DtohisOrder> dtohisOrders = orderService.getHisOrderDataTable(dtoDataTable, pre, actorId);
            jsonList = new DtoDataTableReturn(dtoDataTable.getDraw(), count, count, dtohisOrders);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return jsonList;
    }

    @ResponseBody
    @RequestMapping("/chageOrderStatus")
    public DtoAjaxMsg getOrderStatus(Integer orderId,Integer orderStatus) {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result;
            result = orderService.chageOrderStatus(orderId,orderStatus);
            if (result != 0) {
                dtoAjaxMsg.setAjaxInfo("Success");
            } else {
                dtoAjaxMsg.setAjaxInfo("Faile");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dtoAjaxMsg;
    }
}
